// src/pages/internship/report/apis/mock.ts
import type {
  CreateOrUpdateSummaryRequest,
  ReviewSummaryRequest,
  SummaryQuery,
  SummaryReport,
  SummaryTask
} from "./type"

// 模拟总结任务数据
const MOCK_SUMMARY_TASKS: SummaryTask[] = [
  {
    id: 1,
    studentId: 1001,
    studentName: "张三",
    positionId: 1,
    positionName: "前端开发实习生",
    cycleType: "weekly",
    cycleName: "第1周",
    startTime: "2024-01-15",
    endTime: "2024-01-21",
    deadline: "2024-01-21 23:59:59",
    remainingDays: 3,
    status: "pending"
  },
  {
    id: 2,
    studentId: 1001,
    studentName: "张三",
    positionId: 1,
    positionName: "前端开发实习生",
    cycleType: "weekly",
    cycleName: "第2周",
    startTime: "2024-01-22",
    endTime: "2024-01-28",
    deadline: "2024-01-28 23:59:59",
    remainingDays: 10,
    status: "pending"
  },
  {
    id: 3,
    studentId: 1002,
    studentName: "李四",
    positionId: 2,
    positionName: "人工智能算法实习生",
    cycleType: "monthly",
    cycleName: "第1月",
    startTime: "2024-01-01",
    endTime: "2024-01-31",
    deadline: "2024-02-05 23:59:59",
    remainingDays: 12,
    status: "pending"
  }
]

// 模拟总结报告数据
const MOCK_SUMMARY_REPORTS: SummaryReport[] = [
  {
    id: 1,
    taskId: 4,
    studentId: 1003,
    workContents: [
      {
        type: "协助开发",
        content: "完成3个接口编写和测试"
      },
      {
        type: "数据整理",
        content: "整理项目所需的测试数据"
      }
    ],
    achievements: "完成2份数据分析报告，优化了登录页面性能",
    achievementFiles: ["/mock/files/report1.pdf", "/mock/files/report2.pdf"],
    problems: [
      {
        problem: "对React Hooks不熟悉",
        solution: "学习B站教程掌握了基础用法"
      }
    ],
    nextPlan: "完成用户中心模块开发，学习状态管理",
    remark: "本周因停电请假1天，已补班",
    status: "approved",
    score: 4,
    comment: "成果清晰，问题解决及时",
    suggestion: "多学习React高级特性",
    submittedAt: "2024-01-14 15:30:00",
    reviewedAt: "2024-01-15 10:20:00",
    reviewerId: 2001,
    reviewerName: "王老师"
  },
  {
    id: 2,
    taskId: 5,
    studentId: 1004,
    workContents: [
      {
        type: "文档编写",
        content: "编写API接口文档"
      }
    ],
    achievements: "编写了部分文档",
    problems: [],
    nextPlan: "继续完善文档",
    status: "rejected",
    comment: "成果未量化，问题描述不具体",
    submittedAt: "2024-01-13 20:15:00",
    reviewedAt: "2024-01-14 09:45:00",
    reviewerId: 2001,
    reviewerName: "王老师"
  }
]

/** 获取学生总结任务列表 */
export function getStudentSummaryTasksMock(query?: any) {
  const filteredTasks = MOCK_SUMMARY_TASKS.filter(task =>
    !query?.status || task.status === query.status
  )

  return Promise.resolve({
    code: 200,
    data: {
      list: filteredTasks,
      total: filteredTasks.length
    },
    message: "success"
  })
}

/** 获取总结任务详情 */
export function getSummaryTaskDetailMock(id: number) {
  const task = MOCK_SUMMARY_TASKS.find(t => t.id === id)
  if (!task) {
    return Promise.resolve({ code: 404, data: null, message: "任务不存在" })
  }
  return Promise.resolve({
    code: 200,
    data: task,
    message: "success"
  })
}

/** 获取总结报告列表（教师审核） */
export function getSummaryReportsMock(query?: SummaryQuery) {
  let filteredReports = [...MOCK_SUMMARY_REPORTS]

  if (query?.status) {
    filteredReports = filteredReports.filter(report => report.status === query.status)
  }

  if (query?.studentName) {
    // 模拟根据学生名称筛选
    filteredReports = filteredReports.filter(() => Math.random() > 0.5)
  }

  return Promise.resolve({
    code: 200,
    data: {
      list: filteredReports,
      total: filteredReports.length
    },
    message: "success"
  })
}

/** 获取总结报告详情 */
export function getSummaryReportDetailMock(id: number) {
  const report = MOCK_SUMMARY_REPORTS.find(r => r.id === id)
  if (!report) {
    return Promise.resolve({ code: 404, data: null, message: "报告不存在" })
  }
  return Promise.resolve({
    code: 200,
    data: report,
    message: "success"
  })
}

/** 创建/更新总结报告 */
export function createOrUpdateSummaryMock(data: CreateOrUpdateSummaryRequest) {
  const task = MOCK_SUMMARY_TASKS.find(t => t.id === data.taskId)
  if (!task) {
    return Promise.resolve({ code: 400, data: null, message: "任务不存在" })
  }

  // 检查是否逾期
  const isOverdue = data.submittedAt ? new Date(data.submittedAt) > new Date(task.deadline) : false

  // 更新任务状态
  task.status = "submitted"
  task.submittedAt = data.submittedAt || new Date().toISOString()

  // 创建新报告并添加到报告数组
  const newReport: SummaryReport = {
    id: Date.now(),
    taskId: data.taskId || task.id, // 确保taskId是必需的number类型
    studentId: data.studentId || task.studentId, // 确保studentId是必需的number类型
    workContents: data.workContents || [], // 确保workContents是必需的数组
    achievements: data.achievements || "", // 确保achievements是必需的字符串
    problems: data.problems || [], // 确保problems是必需的数组
    nextPlan: data.nextPlan || "", // 确保nextPlan是必需的字符串
    // 其他可选字段
    studentName: task.studentName,
    positionName: task.positionName,
    cycleType: task.cycleType,
    status: "pending" as const,
    isOverdue,
    submittedAt: task.submittedAt,
    // 其他可选字段从data中获取
    achievementFiles: data.achievementFiles,
    remark: data.remark,
    overdueReason: data.overdueReason
  }

  // 将新报告添加到模拟数据数组中，使其可以在审核页面显示
  MOCK_SUMMARY_REPORTS.push(newReport)

  return Promise.resolve({
    code: 200,
    data: newReport,
    message: "提交成功"
  })
}

/** 审核总结报告 */
export function reviewSummaryReportMock(id: number, data: ReviewSummaryRequest) {
  const report = MOCK_SUMMARY_REPORTS.find(r => r.id === id)
  if (!report) {
    return Promise.resolve({ code: 404, data: null, message: "报告不存在" })
  }

  // 更新报告状态
  Object.assign(report, {
    ...data,
    reviewedAt: new Date().toISOString(),
    reviewerId: 2001,
    reviewerName: "王老师"
  })

  // 更新对应任务状态
  const task = MOCK_SUMMARY_TASKS.find(t => t.id === report.taskId)
  if (task) {
    task.status = data.status
  }

  return Promise.resolve({
    code: 200,
    data: report,
    message: "审核成功"
  })
}

/** 上传成果附件 */
export function uploadAchievementFileMock(file: File) {
  return Promise.resolve({
    code: 200,
    data: {
      url: `/uploads/achievements/${Date.now()}_${file.name}`
    },
    message: "上传成功"
  })
}
